package com.example.demo.leetcode.classics150;

import java.util.Arrays;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _47最长连续序列
 * @description
 * @date 2023/07/23 9:58
 * <p>
 * ******************************************************
 */
public class _47最长连续序列 {

    public int longestConsecutive(int[] nums) {
        Arrays.sort(nums);

        int max = 0;
        int start = 0;
        int zeroNum = 0;
        for (int i = 0; i < nums.length; i++) {
            if (i == nums.length-1) {
                max = Math.max(max, i - start + 1-zeroNum);
            } else {
                if (nums[i + 1] - nums[i] == 0) {
                    zeroNum++;
                    continue;
                }
                if (nums[i + 1] - nums[i] > 1) {
                    max = Math.max(max, i - start + 1 -zeroNum);
                    start = i + 1;
                    zeroNum = 0;
                }
            }
        }
        return max;
    }


    public static void main(String[] args) {
        _47最长连续序列 s = new _47最长连续序列();
        System.out.println(s.longestConsecutive(new int[]{0, 3, 7, 2, 5, 8, 4, 6, 0, 1}));
    }

}
